package com.hlee.regex;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTestHarness {

    public static void main(String[] args) throws Exception {

		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		
    	while (true) {
    		
    		System.out.println("Enter your regex: ");
    		Pattern pattern = Pattern.compile(cin.readLine());
    		
    		System.out.println("Enter input string to search: ");
            Matcher matcher = pattern.matcher(cin.readLine());

            boolean found = false;
            while (matcher.find()) {
            	System.out.println("I found the text '" + matcher.group() + 
            			"' starting at index " + matcher.start() + 
            			" and ending at index " + matcher.end() + ".");
                found = true;
            }
            if(!found){
                System.out.println("No match found.");
            }
        }
    }
	
	
	
//    public static void main(String[] args){
//        Console console = System.console();
//        if (console == null) {
//            System.err.println("No console.");
//            System.exit(1);
//        }
//        while (true) {
//
//            Pattern pattern = 
//            Pattern.compile(console.readLine("%nEnter your regex: "));
//
//            Matcher matcher = 
//            pattern.matcher(console.readLine("Enter input string to search: "));
//
//            boolean found = false;
//            while (matcher.find()) {
//                console.format("I found the text \"%s\" starting at " +
//                   "index %d and ending at index %d.%n",
//                    matcher.group(), matcher.start(), matcher.end());
//                found = true;
//            }
//            if(!found){
//                console.format("No match found.%n");
//            }
//        }
//    }
}
